﻿/**********************************************
 * 功能名称：中间体计划价格管理
编写人：  杨莹
建立时间： 杨 2010年7月1日
说明：
本页面完成中间体计划价格的维护。

 **************************************************************/

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;
using System.Collections.Generic;

namespace Hisun.View.ProductInProcess
{
    public partial class PIP_PlanPrice : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        public string alert = "";
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button_Edit_Command(object sender, CommandEventArgs e)
        {
            string MyPIPId = e.CommandArgument.ToString();
            string Ap = Application["accountPeriod"].ToString();
            string Ap_Year = Ap.Substring(0, 4);




            var MyEditRow = from a in db.TABC_JS_ProductInProcess_PlanPrice
                            where a.中间体ID == MyPIPId
                            && a.年 == Ap_Year
                            select a;
            int MyCount = MyEditRow.Count();

            if (MyCount == 0)
            {
                GridViewRow myRows = (GridViewRow)((Button)sender).Parent.Parent;
                TABC_JS_ProductInProcess_PlanPrice AddRow = new TABC_JS_ProductInProcess_PlanPrice();
                AddRow.中间体ID = MyPIPId;
                string myPrice = ((TextBox)myRows.Cells[5].FindControl("TextBox_PlanPrice")).Text;

                AddRow.计划价 = Convert.ToDecimal(myPrice);
                AddRow.年 = Ap_Year;

                db.TABC_JS_ProductInProcess_PlanPrice.InsertOnSubmit(AddRow);

                try
                {
                    db.SubmitChanges();
                    alert = "<script>alert('保存成功');</script>";
                }
                catch
                {
                    alert = "<script>alert('保存失败');</script>";
                }
            }
            else if (MyCount == 1)
            {
                GridViewRow myRows = (GridViewRow)((Button)sender).Parent.Parent;
                string myPrice = ((TextBox)myRows.Cells[5].FindControl("TextBox_PlanPrice")).Text;


                MyEditRow.Single().计划价 = Convert.ToDecimal(myPrice);

                try
                {
                    db.SubmitChanges();
                    alert = "<script>alert('保存成功');</script>";
                }
                catch
                {
                    alert = "<script>alert('保存失败');</script>";
                }

            }


            int pageIndx = Convert.ToInt32(CurrentPage.Value);  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            BindMyData(pageIndx);
        }

        private void BindMyData(int PageIndex)
        {
            int skipAccount = PageIndex * 10;

            //List<VABC_JS_ProductAndMeterial> Query1 = new List<VABC_JS_ProductAndMeterial>();

            //List<VABC_JS_ProductAndMeterial> Query2 = new List<VABC_JS_ProductAndMeterial>();

            var PIPList = from a in db.TABC_JS_ProductInProcess
                          join b in db.TABC_JS_CostCenter on a.作业中心ID equals b.作业中心ID
                          select new
                          {
                              a.中间体ID,
                              a.中间体名称,
                              a.作业中心ID,
                              b.作业中心名称,
                              a.计量单位

                          };

            string Ap = Application["accountPeriod"].ToString();
            string Ap_Year = Ap.Substring(0, 4);

            var  Query1 =from a in  PIPList select a;

            if (TextBox_CenterName.Text != "")
            {

                Query1 = from a in PIPList
                          where a.作业中心名称.Contains(TextBox_CenterName.Text)

                          select a;

            }
            else
            {
                Query1 = from a in PIPList
                          select a;


            }

              

            var MyQuery = (from a in  Query1
                           join b in
                               (from c in db.TABC_JS_ProductInProcess_PlanPrice where c.年 == Ap_Year select c) on a.中间体ID  equals b.中间体ID
                           into temps
                           where a.作业中心名称.Contains(TextBox_CenterName.Text)
                           from temp in temps.DefaultIfEmpty()
                           orderby a.作业中心名称,a.中间体名称
                           select new
                           {

                               a.中间体ID,
                               a.作业中心ID,
                               a.作业中心名称,
                               a.中间体名称,
                               a.计量单位,
                               计划价 = (temp == null ? 0 : temp.计划价)

                           }).Skip(skipAccount).Take(10);


            GridView_PIPPlanPrice.DataSource = MyQuery;
            GridView_PIPPlanPrice.DataBind();

            if (MyQuery.Count() == 0)
            {
                Panel_MainQuery.Visible = false;
            }
            else
            {
                Panel_MainQuery.Visible = true;
            }
        }


        //查询出当前记录信息的总条数。
        private int TotalPageAmount()
        {
            int MyQuery = 0;

            var PIPList = from a in db.TABC_JS_ProductInProcess
                          join b in db.TABC_JS_CostCenter on a.作业中心ID equals b.作业中心ID
                          select new
                          {
                              a.中间体ID,
                              a.中间体名称,
                              a.作业中心ID,
                              b.作业中心名称,
                              a.计量单位

                          };

            string Ap = Application["accountPeriod"].ToString();
            string Ap_Year = Ap.Substring(0, 4);

            var Query1 = from a in PIPList select a;

            if (TextBox_CenterName.Text != "")
            {

                Query1 = from a in PIPList
                         where a.作业中心名称.Contains(TextBox_CenterName.Text)

                         select a;

            }
            else
            {
                Query1 = from a in PIPList
                         select a;


            }

            MyQuery = (from a in Query1 select a
                          ).Count();

            return MyQuery;
        }

        protected void MyClick(object sender, EventArgs e)
        {
            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            int Totals = TotalPageAmount();



            int pages = (Totals % 20) == 0 ? (Totals / 20) : (Totals / 20 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "first":

                        pageIndx = 0;

                        break;
                    case "gotopage":
                        if (Convert.ToInt32(TextBox_PageIndex.Text) > pages)
                        {
                            pageIndx = pages - 1;
                            TextBox_PageIndex.Text = pages.ToString();
                        }
                        else
                        {
                            pageIndx = Convert.ToInt32(TextBox_PageIndex.Text) - 1;
                        }

                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            BindMyData(pageIndx);
        }

    }
}
